// subpackage/pages/complaint/detail.js
import requestUtil from '../../../baseRequest'
import requestUrls from '../../../requestUrl'
import imageUtils from '../../../utils/imageUtils.js'
import Notify from '../../../components/vant/notify/notify'
import commonUtils from '../../../utils/util.js'
import Dialog from '../../../components/vant/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{},
    imageList:[],
    complaintOrder:{},
    createdUserName:'',
    replyList:[],
    showLoading:false,
    imagePrefixUrl:requestUrls.baseImagePrefixUrl,
    orderId:'',
  },
  //关闭申诉单
  finishOrder(){
    this.setData({
      showLoading:true,
    })
    Dialog.confirm({
      title: '确认关闭',
      message: '是否确认关闭当前申诉单',
    }).then(() => {
      let formData={
        openId: wx.getStorageSync('openid'),
        criteria:{
          id:this.data.orderId
        }
      }
      requestUtil.postRequest(requestUrls.complaint.finishOrder, formData, (res) => {
        if (!res.hasError) {
          Dialog.alert({
            message: '关闭申诉单成功',
          }).then(() => {
            wx.switchTab({
              url: '/pages/home/index2',
            })
          });    
        }
      }) 
      }).catch(() => {
        console.log("取消关闭申诉");
      });
      this.setData({
        showLoading:false,
      })
  },
  replyOrder(){
    wx.navigateTo({
      url: '/subpackage/pages/complaint/add?orderId='+this.data.orderId,
    })
  },
  previewImage(event) {
    const urls = event.currentTarget.dataset.urls;
    const idx = event.currentTarget.dataset.idx;
    let imageUrls = urls.map(item=>{return this.data.imagePrefixUrl+item.imageUrl});
    wx.previewImage({
      current:idx,
      urls: imageUrls,
    })
  },
  getOrderInfoById(){
    console.log(this.data.userInfo);
    let formData = {
      openId:wx.getStorageSync('openid'),
      criteria:{
        id : this.data.orderId,
      }
    };
    requestUtil.postRequest(requestUrls.complaint.getOrderInfoById, formData, (res) => {
      console.log(res);
      if (!res.hasError) {
        const {imageList=[],replyList=[]} = res.result;
        if(imageList.length>0){
          let imgList =[];
          imageList.forEach(item=>{
            imgList.push({
              ...item,
            })
          })
          console.log(imgList);
          this.setData({
            imageList: imgList,
          })
        }
        this.setData({
          complaintOrder:{...res.result,createdDate:commonUtils.formatDate(res.result.createdDate,"yyyy-MM-dd")},
          createdUserName:res.result.createdUserName,
          replyList,
        })




        
      } else {
        this.showMassage(res.message);
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { orderId} = options;
    this.setData({
      orderId,
      userInfo:getApp().globalData.userInfo,
    });
    this.getOrderInfoById();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})